<template>
 <lay-layout class="Pop">

  <lay-body class="flexrow">
    <div class="w10"></div>
    <lay-field class="left" >
      <template #title>
      <span style="font-size: 16px !important;">基本信息</span>
    </template>    
  <lay-form :model="model" ref="layFormRef" class="MainForm" >   
    <lay-form-item label="id" prop="id" required>
            <lay-input v-model="model.id" :disabled="!isAdd" ></lay-input>
          </lay-form-item>
          <lay-form-item label="所属平台" prop="htplatId" >
            <lay-input v-model="model.htplatName" v-if="!isAdd" disabled></lay-input>
            <lay-select v-model="model.htplatId" v-if="isAdd" 
                    :options="platList"
                  @change="platChange"
                  ></lay-select>
          </lay-form-item>
<lay-form-item label="角色名称" prop="name" required>
            <lay-input v-model="model.name"></lay-input>
          </lay-form-item>
<lay-form-item label="备注" prop="memo" >
            <lay-input v-model="model.memo"></lay-input>
          </lay-form-item>




  </lay-form>
    
</lay-field>
<div class="w10"></div>
<lay-field   class="right" >
      <template #title>
      <span style="font-size: 16px !important;">菜单信息</span>
    </template>   
    
    <lay-form-item label=""     >
      <lay-button size="sm" @click="expandAll(true)">展开全部</lay-button>
      <lay-button size="sm" @click="expandAll(false)">折叠全部</lay-button>
    </lay-form-item>
    
    <lay-form-item label=""     >
      <lay-button size="sm" @click="selectAll()">选中所有</lay-button>
      <lay-button size="sm" @click="selectMenu()">选中菜单</lay-button>
    </lay-form-item>

        <lay-tree
    :data="dataSource"
    :showCheckbox="true"
    v-model:checkedKeys="model.htmenuIds"   
    v-model:expandKeys="expandKeys"
    :checkStrictly="true"
    >
    <template #title="{ data }">
      <div>
        <lay-icon :type="data.imgurl" v-if="data.lx!='权限'"></lay-icon>
        <lay-icon type="layui-icon-lock" v-if="data.lx=='权限'"></lay-icon>
        
        {{ data.title }}
      </div>
	
	</template>

  </lay-tree>

</lay-field>    
<div class="w10"></div>
    </lay-body>
    <lay-footer>
     
      <lay-button size="sm" type="primary" @click="toSubmit"
            >保存</lay-button
          >
          <lay-button size="sm" @click="toCancel" style="margin-left:40px;">取消</lay-button>
    </lay-footer>
  </lay-layout>

</template>

<script lang="ts">
import { ref, watch,reactive,onMounted,h } from 'vue'
import { layer } from '@layui/layer-vue'
import MyHttp from "../../library/MyHttp";  
import MyWin from "../../library/MyWin"; 
import MyPage from "../../library/MyPage"; 
import {MyExcel} from "../../library/MyExcel"; 
import {MySimpleExport,TableColumn} from "../../library/MySimpleExport"; 
import MyData from "../../library/MyData"; 
import add from "./addauth.vue";  

export default {
  props:{
      data:Object,
      isAdd:Boolean
  },

  setup(prop:any, context:any) {
    const baseUrl="/htrole";
    const myWin=ref<any>(null);
    const model = reactive<any>(prop.data )
    const layFormRef = ref<any>()     
    const toSubmit=async()=>{
      layFormRef.value.validate((isValidate:boolean, model:any, errors:Array<any>) => {
          if(!isValidate){
            let msg='';
            errors.forEach(element=>{ 
              msg+=`${element.message}
              `; 
            })
            
            MyWin.alert(msg,{});
            return;
          }
          save();
      });
    } 
    const toCancel=()=>{ 
      context.emit("cancel");  
      myWin.value.close();
    }
    const save=async()=>{
      let url=baseUrl+"/Insert";
      if(!prop.isAdd){
        url=baseUrl+"/update";
      }
      var r=await new MyHttp().post(url,model,
        { 
        }
      ); 
      context.emit("ok",model); 
      myWin.value.close();  
    }

 
    
    const platList=ref<Array<any>>(); 
    const platListQuery=async()=>{
      var r= await new MyHttp().post("/htplat"+"/list",{},
              { 
              }
            ); 
      MyData.toLayOptions(r.data)
      platList.value= r.data; 
       
       
    }
    const platChange=async (data:any)=>{
      queryMenu(0);
    }

    const dataSource=ref<any>();
    const queryMenu=async (pageIndex:number) =>{
      let qm={
        id:model.htplatId
      }
       
      var r=await new MyHttp().post("/htmenu"+"/GetTree",qm,
        { 
        }
      ); 
       
      MyData.toLayTree(r.data); 
      
      dataSource.value=r.data;  
      expandAll(true) ;
    } 

    const expandKeys=ref<any>([]);
    const expandAll=(expanded:boolean)=>{
        if(expanded){
          expandKeys.value=MyData.getTreeIds( dataSource.value) ;
        }
        else{
          expandKeys.value=[];
        }
    }

    onMounted(() =>{  
      myWin.value=new MyWin();
      if(prop.isAdd){
        platListQuery();
      }
      queryMenu(0);
    });
    const selectAll=()=>{
     
      var allids=MyData.getTreeIds(dataSource.value);
      model.htmenuIds=allids; 
    }
    const getAllMenuId=(root:any,r:Array<any>)=>{
      root.forEach( (element:any) => {
        if(element.lx=="目录"){
          r.push(element.id);
          getAllMenuId(element.children,r);
        }else if(element.lx=="菜单"){
          r.push(element.id);
        } 
      });
    }
    const selectMenu=()=>{
      var r=new Array();
      getAllMenuId(dataSource.value,r); 
      model.htmenuIds=r; 
    }
    return {
      model,
      layFormRef,
      toSubmit,
      toCancel, 

      platList, 
      platChange,

      dataSource,
      queryMenu,

      expandKeys,
      expandAll,

      selectAll,
      selectMenu

    }
  }
}
</script>
<style scoped>
.Pop .layui-footer,
.Pop .layui-header {
  height: 50px;
  text-align: center; 
  padding-top:10px;
}  
.Pop .layui-body{
  justify-content:start;
}
.MainForm{
  margin: 0px;
  width:100%;
} 
.left 
{ 
  width: 400px;
}
.right{
  
  flex:1;
  
}
.w10{
  width:10px;
}
.right .TBMain{
  width:380px;
}
.right .layui-form-label{
  width:60px;
}
.right .layui-form-item{
  margin-bottom: 5px;
}

 
.Item{
  border-bottom:1px dashed var(--global-neutral-color-5) ;
  margin-top:10px;
}
.Item:hover  {
  box-shadow: 2px 2px 14px #ccc;
  cursor: pointer;
}
.Item:hover i{
  display: inline;
}
</style>